package org.jeeasy.common.core.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.jeeasy.common.core.domain.model.QueryPageModel;
import org.jeeasy.common.core.exception.JeeasyException;
import org.jeeasy.common.core.tools.GenericUtil;
import org.jeeasy.common.core.tools.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

/**
 * @author Alps
 */
@Slf4j
//@RequestMapping("api")
public class SimpleCurdController<S extends IService<T>, T> implements GenericUtil<T> {

    @Autowired(required = false)
    protected S service;

    private final Class<T> modelClazz;

    public SimpleCurdController() {
        this.modelClazz = this.getGenericSuperclass(this.getClass(), 1);
    }

    protected QueryWrapper<T> getWrapper() {
        return QueryGenerator.ofWrapper(modelClazz, null);
    }

    protected QueryWrapper<T> getWrapper(QueryPageModel query) {
        return QueryGenerator.ofWrapper(modelClazz, query);
    }

    /**
     * 列表分页查询
     *
     * @param query
     * @return {@link IPage<T>}
     * @author mobie
     * @date 2020/11/21 16:24
     */
    protected IPage<T> queryPage(QueryPageModel query, QueryWrapper<T> wrapper) {
        return service.page(query.getPage(), wrapper);
    }


    /**
     * 列表分页查询
     *
     * @param query
     * @return {@link IPage<T>}
     * @author mobie
     * @date 2020/11/21 16:24
     */
    protected IPage<T> queryPage(QueryPageModel query) {
        return queryPage(query, getWrapper(query));
    }

    /**
     * 通过id获取对象
     *
     * @param id
     * @return {@link T}
     * @author mobie
     * @date 2020/11/21 16:22
     */
    protected T getById(Serializable id) {
        return service.getById(id);
    }

    /**
     * 保存对象
     *
     * @param entity
     * @author mobie
     * @date 2020/11/21 16:23
     */
    protected void insert(T entity) {
        if (!service.save(entity)) {
            throw new JeeasyException("新增失败");
        }
    }

    /**
     * 修改对象
     *
     * @param entity
     * @author mobie
     * @date 2020/11/21 16:24
     */
    protected void update(T entity) {
        if (!service.updateById(entity)) {
            throw new JeeasyException("修改失败");
        }
    }

    /**
     * 根据id删除对象
     *
     * @param id
     * @author mobie
     * @date 2020/11/21 16:24
     */
    protected void deleteById(Serializable id) {
        if (!service.removeById(id)) {
            throw new JeeasyException("删除失败");
        }
    }

    /**
     * 根据ids批量删除对象
     *
     * @param ids
     * @author mobie
     * @date 2020/11/21 16:24
     */
    protected void batchDelete(@NonNull String ids) {
        if (!service.removeByIds(List.of(ids.split(",")))) {
            throw new JeeasyException("删除成功");
        }
    }
}